﻿using Microsoft.ServiceBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;

namespace OnPremiseProductService
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting...");

            if (WebRequest.DefaultWebProxy.Credentials == null)
            {
                WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
            }

            ServiceBusEnvironment.SystemConnectivity.Mode = GetConnectivityMode(args);


            string serviceNamespace = "pijushbus";
            string issuerName = "owner";
            string issuerSecret = "SUu1kESNR3X/YlfgSfFMBWinVX+Y5IW5Jqpqb6hBJ4k=";

            // create the service URI based on the service namespace
            Uri address = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "HelliumService");

            // create the credentials object for the endpoint
            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);

            // individual listen URI suffix for this instance.
            string serviceInstanceId = Guid.NewGuid().ToString("N");

            // create the service host
            ServiceHost host = new ServiceHost(typeof(HelliumService));

            // add the endpoint
            Uri instanceListenUri = new Uri(address, serviceInstanceId + "/");
            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IHelliumService), new NetTcpRelayBinding(EndToEndSecurityMode.None, RelayClientAuthenticationType.RelayAccessToken), address, instanceListenUri);

            // create the ServiceRegistrySettings behavior for the endpoint
            IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);

            // add the Service Bus credentials to all endpoints specified in configuration
            endpoint.Behaviors.Add(serviceRegistrySettings);
            endpoint.Behaviors.Add(sharedSecretServiceBusCredential);

            Console.WriteLine("Service address: " + endpoint.Address.Uri);
            Console.WriteLine("Listen address: " + endpoint.ListenUri);

            // open the service
            host.Open();

            Console.WriteLine("Press [Enter] to exit");
            Console.ReadLine();

            // close the service
            host.Close();
       }

        static ConnectivityMode GetConnectivityMode(string[] args)
        {
            foreach (string arg in args)
            {
                if (arg.Equals("/auto", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-auto", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.AutoDetect;
                }
                else if (arg.Equals("/tcp", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-tcp", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.Tcp;
                }
                else if (arg.Equals("/http", StringComparison.InvariantCultureIgnoreCase) ||
                     arg.Equals("-http", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ConnectivityMode.Http;
                }
            }
            return ConnectivityMode.AutoDetect;
        }
    }
}
